﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Baula.TextGen.Framework
{
    public class ValuesForPlaceholders : IEnumerable<KeyValuePair<string, string>>
    {
        private readonly Dictionary<string, string> _valuesForPlaceholderDictionary = new Dictionary<string, string>();
        private readonly ReadOnlyCollection<string> _placeholdersInTemplate;

        public ValuesForPlaceholders(ReadOnlyCollection<string> placeholdersInTemplate)
        {
            _placeholdersInTemplate = placeholdersInTemplate;
        }

        public string this[string placeholder]
        {
            get { return _valuesForPlaceholderDictionary[placeholder]; }
            set
            {
                if (!PlaceholdersInTemplate.Contains(placeholder))
                {
                    throw new ArgumentException(string.Format("There is no placeholder \"{0}\".", placeholder));
                }
                _valuesForPlaceholderDictionary.Add(placeholder, value);
            }
        }

        private ReadOnlyCollection<string> PlaceholdersInTemplate
        {
            get { return _placeholdersInTemplate; }
        }

        private Dictionary<string, string> ValuesForPlaceholdersDictionary
        {
            get { return _valuesForPlaceholderDictionary; }
        }

        public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
        {
            return ValuesForPlaceholdersDictionary.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public bool Contains(string placeholder)
        {
            return ValuesForPlaceholdersDictionary.ContainsKey(placeholder);
        }
    }
}
